﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace BEGDC.CC.Web
{
	/// <summary>
	/// Summary description for SetBackuper.
	/// </summary>
	public class SetBackuper : System.Web.UI.Page
	{
		private void Page_Load(object sender, System.EventArgs e)
		{
			BEGDC.CC.BL.Workflow.CCPrincipal principal = (BEGDC.CC.BL.Workflow.CCPrincipal)this.Page.User;
			string backupid = string.Empty;
			try
			{
				if (this.Request.QueryString["backupid"] != null)
				{				
					backupid = (this.Request.QueryString["backupid"]);				
				}
				if (backupid == "0")
				{
					principal.ClearBackup();

					//Log
					BEGDC.CC.BL.Common.IUtility utility = BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.CC.BL.Common.ServiceConst.UtilityService] as BEGDC.CC.BL.Common.IUtility;
					BEGDC.CC.BL.Workflow.CCIdentity id = this.User.Identity as BEGDC.CC.BL.Workflow.CCIdentity;
					BEGDC.CC.Entity.OperationLog log = new BEGDC.CC.Entity.OperationLog();
					log.Module = "UserManagement";
					BEGDC.CC.Entity.Employee emp = new BEGDC.CC.Entity.Employee();
					emp.Id = id.UserInfo.Id;
					log.Employee = emp;
					log.Time = System.DateTime.Now;
					log.Description = "End delegate. User:" + ((BEGDC.CC.BL.Workflow.CCIdentity)principal.Identity).UserInfo.NameCN ;
					utility.SaveOperationLog(log);
				}
				else
				{
					BEGDC.CC.BL.Workflow.CCIdentity identity = principal.Identity as BEGDC.CC.BL.Workflow.CCIdentity;
					for(int i=0; i<	identity.Backupers.Count; i++)
					{
						BEGDC.CC.Entity.Employee user = (BEGDC.CC.Entity.Employee)identity.Backupers[i];
						if (user.Id == backupid)
						{
							//Log
							BEGDC.CC.BL.Common.IUtility utility = BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.CC.BL.Common.ServiceConst.UtilityService] as BEGDC.CC.BL.Common.IUtility;
							BEGDC.CC.BL.Workflow.CCIdentity id = this.User.Identity as BEGDC.CC.BL.Workflow.CCIdentity;
							BEGDC.CC.Entity.OperationLog log = new BEGDC.CC.Entity.OperationLog();
							log.Module = "UserManagement";
							BEGDC.CC.Entity.Employee emp = new BEGDC.CC.Entity.Employee();
							emp.Id = id.UserInfo.Id;
							log.Employee = emp;
							log.Time = System.DateTime.Now;
							log.Description = "Start delegate. User:" + identity.UserInfo.NameCN + ". Delegation:" + user.NameCN ;
							utility.SaveOperationLog(log);

							principal.SetBackup(user);

						}
					}
				}
			}
			catch
			{
				principal.ClearBackup();
			}
			this.Response.Redirect("..\\left.aspx", true);
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion
	}
}
